<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${info.daoPackage}.${info.daoName}">
    <resultMap id="resultEntity" type="${info.modelPackage}.${info.modelName}">
        <#if info.primarykeyFields??>
        <id column="${info.primarykeyFields.fieldName}" property="${info.primarykeyFields.propertyName}" jdbcType="${info.primarykeyFields.fieldTypeXml}" />
        </#if>
        <#list info.fields as field>
		<result column="${field.fieldName}" property="${field.propertyName}" jdbcType="${field.fieldTypeXml}" />
        </#list>
    </resultMap>
    <sql id="column_List">
        <![CDATA[
        <#if info.primarykeyFields??>
            ${info.primarykeyFields.fieldName}<#list info.fields as field>,${field.fieldName}</#list>
        <#else >
            <#list info.fields as field><#if field_index == 0>${field.fieldName}<#else >,${field.fieldName}</#if></#list>
        </#if>
        ]]>
    </sql>
    <sql id="Entity_Where_Clause">
        <where>
            <trim prefix="(" suffix=")" prefixOverrides="and">
                <#if info.primarykeyFields??>
                <if test="${info.primarykeyFields.condition} ">
                    and ${info.primarykeyFields.fieldName}=#\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
                </if>
                </#if>
                <#list info.fields as field>
                <if test="${field.condition}">
                    and  ${field.fieldName}=#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}
                </if>
                </#list>
            </trim>
        </where>
    </sql>

    <sql id="Map_Where_Clause">
        <where>
            <trim prefix="(" suffix=")" prefixOverrides="and">
                <#if info.primarykeyFields??>
                <if test="${info.primarykeyFields.condition} ">
                    and ${info.primarykeyFields.fieldName}=#\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
                </if>
                </#if>
                <#list info.fields as field>
                <if test="${field.condition}">
                    and  ${field.fieldName}=#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}
                </if>
                </#list>
                <#if info.primarykeyFields??>
                <if test="ids != null and ids.size() > 0">
                    and ${info.primarykeyFields.fieldName} in
                    <foreach item="idItem" collection="ids" open="(" separator="," close=")">
                        #\{idItem}
                    </foreach>
                </if>
                </#if>
            </trim>
        </where>
    </sql>
    <#if info.primarykeyFields??>
    <select id="selectByPrimaryKey" resultMap="resultEntity" parameterType="${info.primarykeyFields.javaTypeName}">
        select
        <include refid="column_List" />
        from ${info.tableName}
        where ${info.primarykeyFields.fieldName} = #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
    </select>
    </#if>
    <select id="selectByEntityWhere" resultMap="resultEntity" parameterType="${info.modelPackage}.${info.modelName}">
        select
        <include refid="column_List" />
        from ${info.tableName}
        <include refid="Entity_Where_Clause" />
    </select>
    <select id="selectByMapWhere" resultMap="resultEntity" parameterType="java.util.Map">
        select
        <include refid="column_List" />
        from ${info.tableName}
        <include refid="Map_Where_Clause" />
        <if test="orderCondition != null and orderCondition != ''" >
            order by $\{orderCondition}
        </if>
        <if test="queryCondition != null and queryCondition != ''" >
            $\{queryCondition}
        </if>
    </select>
    <#if info.primarykeyFields??>
    <delete id="deleteByPrimaryKey" parameterType="${info.primarykeyFields.javaTypeName}">
        <![CDATA[
        delete from ${info.tableName}
        where ${info.primarykeyFields.fieldName} = #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
        ]]>
    </delete>
    </#if>
    <#if info.primarykeyFields??>
    <delete id="batchDeleteByIds" parameterType="${info.primarykeyFields.javaTypeName}">
        delete FROM ${info.tableName} where ${info.primarykeyFields.fieldName} in
        <foreach item="idItem" collection="ids" open="(" separator="," close=")">
            #\{idItem}
        </foreach>
    </delete>
    </#if>
    <insert id="insert" parameterType="${info.modelPackage}.${info.modelName}">
        <#if info.primarykeyFields??>
        <!--
    	<selectKey keyProperty="${info.primarykeyFields.propertyName}" resultType="${info.primarykeyFields.javaTypeName}" order="BEFORE">  
            SELECT ${info.tableName}_Seq.nextval ${info.primarykeyFields.propertyName} FROM dual
        </selectKey>  
        -->
        </#if>
        <![CDATA[
        insert into ${info.tableName} (
            <#if info.primarykeyFields??>
            ${info.primarykeyFields.fieldName}<#list info.fields as field>,${field.fieldName}</#list>
            <#else >
            <#list info.fields as field><#if field_index == 0>${field.fieldName}<#else >,${field.fieldName}</#if></#list>
            </#if>
        )
        values (
            <#if info.primarykeyFields??>
            #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}<#list info.fields as field>,#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}</#list>
            <#else >
            <#list info.fields as field><#if field_index == 0>#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}<#else >,#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}</#if></#list>
            </#if>
        )
        ]]>
    </insert>
    <insert id="insertSelective" parameterType="${info.modelPackage}.${info.modelName}">
        <#if info.primarykeyFields??>
        <!--
    	<selectKey keyProperty="${info.primarykeyFields.propertyName}" resultType="${info.primarykeyFields.javaTypeName}" order="BEFORE"> 
	        SELECT ${info.tableName}_Seq.nextval ${info.primarykeyFields.propertyName} FROM dual
        </selectKey>
        -->
        </#if>
        insert into ${info.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#if info.primarykeyFields??>
            <if test="${info.primarykeyFields.condition} ">
                ${info.primarykeyFields.fieldName},
            </if>
            </#if>
            <#list info.fields as field>
			<if test="${field.condition}">
                ${field.fieldName},
            </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#if info.primarykeyFields??>
            <if test="${info.primarykeyFields.condition}">
                #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}},
            </if>
            </#if>
            <#list info.fields as field>
			<if test="${field.condition}">
                #\{${field.propertyName},jdbcType=${field.fieldTypeXml}},
            </if>
            </#list>
        </trim>
    </insert>

    <insert id="batchInsert" parameterType="${info.modelPackage}.${info.modelName}">
        insert into ${info.tableName} (
        <#if info.primarykeyFields??>
            ${info.primarykeyFields.fieldName}<#list info.fields as field>,${field.fieldName}</#list>
        <#else >
            <#list info.fields as field><#if field_index == 0>${field.fieldName}<#else >,${field.fieldName}</#if></#list>
        </#if>
        )
        values
        <foreach item="idItem" collection="list" open="" separator="," close="">
            (
            <#if info.primarykeyFields??>
            #\{idItem.${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
            <#list info.fields as field>,#\{idItem.${field.propertyName},jdbcType=${field.fieldTypeXml}}</#list>
            <#else >
            <#list info.fields as field><#if field_index == 0>#\{idItem.${field.propertyName},jdbcType=${field.fieldTypeXml}}<#else >,#\{idItem.${field.propertyName},jdbcType=${field.fieldTypeXml}}</#if></#list>
            </#if>
            )
        </foreach>

    </insert>
    <#if info.primarykeyFields??>
    <update id="updateByIdSelective" parameterType="${info.modelPackage}.${info.modelName}">
        update ${info.tableName}
        <set>
        <#list info.fields as field>
            <if test="${field.condition}">
                ${field.fieldName}=#\{${field.propertyName},jdbcType=${field.fieldTypeXml}},
            </if>
        </#list>
        </set>
        where  ${info.primarykeyFields.fieldName}= #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml }}
    </update>
    <update id="updateByPrimaryKey" parameterType="${info.modelPackage}.${info.modelName}">
        update ${info.tableName}
        set 
        <#list info.fields as field>
            <#if field_index!=0>,</#if>${field.fieldName}=#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}
        </#list>
        where  ${info.primarykeyFields.fieldName}= #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
    </update>
    </#if>
    <!-- 
   	 附加信息，便于自己写sql语句
   	 <#if info.primarykeyFields??>
    ${info.primarykeyFields.fieldName}= #\{${info.primarykeyFields.propertyName},jdbcType=${info.primarykeyFields.fieldTypeXml}}
     </#if>
    <#list info.fields as field>
	${field.fieldName}=#\{${field.propertyName},jdbcType=${field.fieldTypeXml}}
    </#list>
     -->

</mapper>